/*
 * @Copyright: Copyright (C) AMCAX, All rights reserved.
 * @Author: 殷建峰
 * @Date: 2025-02-05 10:30:33
 * @LastEditTime: 2025-02-05 19:15:17
 * @LastEditors: 殷建峰
 * @Description:
 */
import { Tabs } from 'antd';
import styles from './styles.module.scss';
import { useMemo } from 'react';
import { useWidgetStore } from '@/common/store';
import { LeftPanelMenu } from '@/layouts/LeftPanels';
import { i18n } from '@/common/utils';
import { widgetManager } from '@/common/managers/widgetManager';
export default function LeftBar() {
  const { hisSelection, selectId } = useWidgetStore();
  const onChange = (activeKey: string) => {
    widgetManager.setSelectId(activeKey);
  };

  const hisMenu = useMemo(() => {
    return hisSelection.map((it) => {
      const temp = LeftPanelMenu.find(
        (item) => it === item.id && item.type === 'panel',
      );
      return {
        label: i18n.t(temp?.label),
        key: it,
        children: temp?.create() || <></>,
      };
    });
  }, [hisSelection]);
  const onEdit = (
    targetKey: React.MouseEvent | React.KeyboardEvent | string,
    action: 'add' | 'remove',
  ) => {
    if (action === 'remove') {
      const menuIds = new Set<string>(hisSelection);
      menuIds.delete(targetKey as string);
      const temp = Array.from(menuIds);
      widgetManager.updataHisSelection(temp);
      if (selectId === (targetKey as string)) {
        widgetManager.setSelectId(temp[temp.length - 1]);
      }
    }
  };
  return (
    <div className={styles.leftBar}>
      <Tabs
        className={styles.tabs}
        hideAdd
        type='editable-card'
        size='small'
        activeKey={selectId}
        onChange={onChange}
        onEdit={onEdit}
        items={hisMenu}></Tabs>
    </div>
  );
}
